package com.ruoyi.manage.controller;

import java.io.File;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.manage.domain.MusicSongpages;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.manage.domain.MusicSongs;
import com.ruoyi.manage.service.IMusicSongsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 歌曲信息Controller
 *
 * @author xiaoshier
 * @date 2024-07-29
 */
@RestController
@RequestMapping("/MusicSongs/songs")
public class MusicSongsController extends BaseController
{
    @Autowired
    private IMusicSongsService musicSongsService;


    /**
     * 查询歌曲信息列表
     */
    @PreAuthorize("@ss.hasPermi('MusicSongs:songs:list')")
    @GetMapping("/list")
    public TableDataInfo list(MusicSongs musicSongs)
    {
        startPage();
        List<MusicSongs> list = musicSongsService.selectMusicSongsList(musicSongs);
        return getDataTable(list);
    }

    /**
     * 导出歌曲信息列表
     */
    @PreAuthorize("@ss.hasPermi('MusicSongs:songs:export')")
    @Log(title = "歌曲信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MusicSongs musicSongs)
    {
        List<MusicSongs> list = musicSongsService.selectMusicSongsList(musicSongs);
        ExcelUtil<MusicSongs> util = new ExcelUtil<MusicSongs>(MusicSongs.class);
        util.exportExcel(response, list, "歌曲信息数据");
    }

    /**
     * 获取歌曲信息
     */
    @PreAuthorize("@ss.hasPermi('MusicSongs:songs:query')")
    @GetMapping(value = "/{musicSongsId}")
    public AjaxResult getInfo(@PathVariable("musicSongsId") Long musicSongsId)
    {
        return success(musicSongsService.selectMusicSongsByMusicSongsId(musicSongsId));
    }



    /**
     * 新增歌曲信息
     */
    @PreAuthorize("@ss.hasPermi('MusicSongs:songs:add')")
    @Log(title = "歌曲信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MusicSongs musicSongs)
    {
        return toAjax(musicSongsService.insertMusicSongs(musicSongs));
    }

    /**
     * 修改歌曲信息
     */
    @PreAuthorize("@ss.hasPermi('MusicSongs:songs:edit')")
    @Log(title = "歌曲信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MusicSongs musicSongs)
    {
        return toAjax(musicSongsService.updateMusicSongs(musicSongs));
    }

    /**
     * 删除歌曲信息
     */
    @PreAuthorize("@ss.hasPermi('MusicSongs:songs:remove')")
    @Log(title = "歌曲信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{musicSongsIds}")
    public AjaxResult remove(@PathVariable Long[] musicSongsIds)
    {
        List<MusicSongpages> list = musicSongsService.selectMusicSongpages(musicSongsIds);
        for (MusicSongpages item : list) {
            String pageUrl = item.getPageUrl();
            deleteFile(pageUrl);
        }
        return toAjax(musicSongsService.deleteMusicSongsByMusicSongsIds(musicSongsIds));
    }

    /**
     * 删除歌曲图片
     */
    public static boolean deleteFile(String filePath) {
        String localPath = RuoYiConfig.getProfile();
        String downloadPath = localPath + StringUtils.substringAfter(filePath, Constants.RESOURCE_PREFIX);
        boolean flag = false;
        File file = new File(downloadPath);
        if (file.isFile() && file.exists()) {
            file.delete();
            flag = true;
        }
        return flag;
    }

}
